package com.cacf.uddi.clientExample;

import inspireit.uddi.BusinessEntity;
import inspireit.uddi.base.BusinessKey;
import inspireit.uddi.base.Name;
import inspireit.uddi.base.UDDIConstants;
import inspireit.uddi.base.UDDIException;
import inspireit.uddi.collections.BusinessEntitys;
import inspireit.uddi.collections.Results;
import inspireit.uddi.request.UDDIProfile;
import inspireit.uddi.request.UDDIPublisher;
import inspireit.uddi.response.BusinessDetail;
import inspireit.uddi.response.DispositionReport;
import inspireit.uddi.response.base.Result;

import java.net.URL;

/**
 * This example shows how to save a business entity. You will have to open an
 * account at one of the official UDDI registries if you don't have access to a
 * local UDDI registry. For example, you can use the beta registry of IBM
 * located at http://uddi.ibm.com/beta/registry.html.
 * <p>
 * 
 * If the size of your account is limited, you will have to make a clean-up
 * first, for example via the Web interface of the on-line registry.
 * 
 * @author Bertrand Fontaine, <a href="http://www.inspireit.biz"
 *         target="INSPIREIT">INSPIRE IT</a>
 */
public class SaveBusinessExample {

	protected BusinessKey businessKey = null;

	public SaveBusinessExample(String queryURL, String publishURL,
			String securityURL, String username, String password)
			throws Exception {
		System.setProperty("java.protocol.handler.pkgs",
				"com.sun.net.ssl.internal.www.protocol");
		java.security.Security
				.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		// we define a profile to connect to the UDDI registry
		UDDIProfile profile = new UDDIProfile();
		profile.setQueryURL(new URL(queryURL));
		profile.setPublishURL(new URL(publishURL));
		if (securityURL != null)
			profile.setSecurityURL(new URL(securityURL));
		profile.setUsername(username);
		profile.setPassword(password);

		// uncomment one of the following lines to use UDDI V2 or V3 messaging
		// instead of the default V1
		// profile.setVersion(UDDIConstants.UDDI_V2);
		profile.setVersion(UDDIConstants.UDDI_V3);

		try {
			// get an appropriate publisher
			UDDIPublisher publisher = UDDIPublisher.getPublisher(profile);

			// build up the BusinessEntity to save in the registry
			Name name = new Name("INSPIRE IT UDDI - Sample Business");
			BusinessKey businessKey = null; // it's a new business...
			BusinessEntity businessEntity = new BusinessEntity(businessKey,
					name);

			// execute the SaveBusiness operation and get back the
			// BusinessDetail
			BusinessDetail businessDetail = publisher
					.saveBusiness(businessEntity);

			// get the saved BusinessEntity
			BusinessEntitys businessEntities = businessDetail
					.getBusinessEntitys();
			BusinessEntity savedBusinessEntity = (businessEntities.toArray())[0];

			this.businessKey = savedBusinessEntity.getBusinessKey();
			System.out.println("BusinessEntity saved with business key : "
					+ this.businessKey);
		} catch (UDDIException e) {
			System.out
					.println("An error has occured. More information follows.");
			DispositionReport dispositionReport = e.getDispositionReport();

			if (dispositionReport != null) {
				Results results = dispositionReport.getResults();
				Result result[] = results.toArray();

				for (int i = 0; i < result.length; i++) {
					System.out.println("Errno   : " + result[i].getErrNo()
							+ "\n KeyType : " + result[i].getKeyType()
							+ "\n ErrInfo : " + result[i].getErrInfo());
				}
			} else {
				System.out.println(e.getMessage());
			}
		}
	}

	public BusinessKey getBusinessKey() {
		return businessKey;
	}

	public static void main(String args[]) throws Exception {
		// if ((args.length != 4) && (args.length != 5)) {
		// System.out.println("SaveBusinessExample usage: java SaveBusinessExample <queryURL> <publishURL> [<securityURL>] <username> <password>");
		// System.exit(0);
		// }

		String queryURL = "http://localhost:8080/juddiv3/services/inquiry";
		String publishURL = "http://localhost:8080/juddiv3/services/publish";
		String securityURL = "http://localhost:8080/juddiv3/services/security";
		String username = "root";
		String password = "root";

		// if (args.length == 4) {
		// username = args[2];
		// password = args[3];
		// } else { // must be 5 ...
		// securityURL = args[2];
		// username = args[3];
		// password = args[4];
		// }

		new SaveBusinessExample(queryURL, publishURL, securityURL, username,
				password);

		return;
	}
}